//
//  ZYCTeachingTeachLearnStatisticsViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/28.
//

#import "ZYCTeachingTeachLearnStatisticsViewController.h"
#import "ZYCProtalCalendarStatisticsCell.h"
#import "ZYCTeachLearnStatisticsSliderView.h"
#import "ZYCTeachLearnStatisticsModel.h"
#import "ZYCNoDataView.h"
@interface ZYCTeachingTeachLearnStatisticsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *titlesArray;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) ZYCTeachLearnStatisticsSliderView *menuSliderView;
@property (nonatomic, assign) bool isSubmit;

@property (nonatomic, assign) NSInteger submitCount;
@property (nonatomic, assign) NSInteger unSubmitCount;
@property (nonatomic, strong) ZYCNoDataView *noDataView;
@end

@implementation ZYCTeachingTeachLearnStatisticsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.titleString;
    [self.view addSubview:self.menuSliderView];
    [self.view addSubview:self.tableView];
    self.isSubmit = YES;
    [self loadQueryCheckList];
}

- (void)loadQueryCheckList{
    
    NSDictionary *params = @{
        @"quEvalutionId":self.quEvalutionId
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_QueryCheckList requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                NSArray *array = [NSArray yy_modelArrayWithClass:[ZYCTeachLearnStatisticsModel class] json:responseObject[@"body"]];
                NSMutableArray *submitList = [NSMutableArray arrayWithCapacity:0];
                NSMutableArray *unSubmitList = [NSMutableArray arrayWithCapacity:0];
                for (ZYCTeachLearnStatisticsModel *model in array) {
                    if ([model.checkFlag integerValue] == 0) {// 未提交
                        [unSubmitList addObject:model];
                    }else{//已提交
                        [submitList addObject:model];
                    }
                }
                self.submitCount = submitList.count;
                self.unSubmitCount = unSubmitList.count;
                
                self.menuSliderView.leftString = [NSString stringWithFormat:@"已提交(%zd)",self.submitCount];
                self.menuSliderView.rightString = [NSString stringWithFormat:@"未提交(%zd)",self.unSubmitCount];
                
                if (self.isSubmit) {
                    [self.dataSource removeAllObjects];
                    [self.dataSource addObjectsFromArray:submitList];
                }else{
                    [self.dataSource removeAllObjects];
                    [self.dataSource addObjectsFromArray:unSubmitList];
                }
                [self.tableView reloadData];
                if (self.dataSource.count == 0) {
                    self.tableView.hidden = YES;
                    self.noDataView.hidden = NO;
                }else{
                    self.tableView.hidden = NO;
                    self.noDataView.hidden = YES;
                }
                

            }else{
                [MrjToast makeToast:responseObject[@"messgae"] makeView:self.view];
            }

            
        } failure:^(NSError * _Nonnull error) {
            
        }];
}

- (ZYCTeachLearnStatisticsSliderView *)menuSliderView {
    ZYCWeakSelf
    if (!_menuSliderView) {
        _menuSliderView = [[ZYCTeachLearnStatisticsSliderView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, 46.0f)];
        _menuSliderView.selectTitleBlock = ^(NSInteger tag) {
            if (tag == 1000) {
                weakSelf.isSubmit = YES;
            }else{
                weakSelf.isSubmit = NO;
            }
            [weakSelf loadQueryCheckList];
        };
    }
    return _menuSliderView;
}

- (ZYCNoDataView *)noDataView {
    if (!_noDataView) {
        _noDataView = [[ZYCNoDataView alloc] initWithFrame:self.tableView.frame];
        _noDataView.hidden = YES;
        [self.view addSubview:_noDataView];
    }
    return _noDataView;
}



#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCProtalCalendarStatisticsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCProtalCalendarStatisticsCell class]) forIndexPath:indexPath];
    cell.tModel = self.dataSource[indexPath.section];
    cell.isRead = self.isSubmit;
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.0;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.menuSliderView.frame), kScreenWidth, kScreenHeight - CGRectGetMaxY(self.menuSliderView.frame)) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCProtalCalendarStatisticsCell class] forCellReuseIdentifier:NSStringFromClass([ZYCProtalCalendarStatisticsCell class])];
    }
    return _tableView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSource;
}


- (NSArray *)titlesArray {
    if (!_titlesArray) {
        _titlesArray = @[@"已提交",@"未提交"];
    }
    return _titlesArray;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
